package com.hoyo.web;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.hoyo.common.PageInfo;
import com.hoyo.config.UserRealm.Principal;
import com.hoyo.domain.Catalog;
import com.hoyo.domain.Channel;
import com.hoyo.domain.User;
import com.hoyo.domain.UserInfo;
import com.hoyo.service.CatalogService;
import com.hoyo.service.ChannelService;
import com.hoyo.service.UserService;

@Controller
public class ChannelController {
	
	@Autowired
	private ChannelService channelService;
	
	@Autowired
	private CatalogService catalogService;
	
	@Autowired
	private UserService userService;
	
	@RequestMapping("/channel/channels")
	@ResponseBody
	public String channels(){
		JSONObject obj = new JSONObject();
		try {
			List<Channel> channels = channelService.findAllChannels();
			obj.put("channels", channels);
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return obj.toString();
	}
	
	@RequestMapping("/channel/{channelId}")
	public ModelAndView channelMAV(@PathVariable("channelId") int channelId,@RequestParam(value="sortType", defaultValue="1") int sortType) throws Exception{
		//Principal Principal = (Principal) SecurityUtils.getSubject().getPrincipal();
		ModelAndView mav = new ModelAndView("channel.html");
		List<Channel> channels = channelService.findAllChannelsAndCountMovieInEveryChannel();
		PageInfo<Catalog> pages = catalogService.pageFindCatalogsByChannelId(channelId, 1, 20, sortType);
		List<Catalog> catalogs = catalogService.findCatalogsByChannelsTypeAndSortByPlayAmount(channelId, 0, 10);
/*		if(Principal!=null){
			User user = userService.findByIdentifier(Principal.getTelephone());
			UserInfo userInfo = userService.findUserInfoByUserId(user.getId());
			mav.addObject("user", user);
			mav.addObject("userInfo", userInfo);
		}*/
		mav.addObject("channels", channels);
		mav.addObject("sortType", sortType);
		mav.addObject("channelId", channelId);
		mav.addObject("pages",pages);
		mav.addObject("catalogs", catalogs);
		return mav;
	}
	
	@RequestMapping("/admin/channel/save")
	@ResponseBody
	public String save(@RequestParam("id")int id, @RequestParam("channelName") String channelName
			, @RequestParam("description") String description){
		JSONObject obj = new JSONObject();
		try {
			PageInfo<Channel> pageInfo = channelService.save(id, channelName, description);
			obj.put("pageInfo", pageInfo);
			obj.put("success", true);
		}catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		return obj.toString();
	}
	
	@RequestMapping("/admin/channel/channels")
	@ResponseBody
	public String adminChannels(@RequestParam("pageNum")int pageNum, @RequestParam("pageSize") int pageSize){
		JSONObject obj = new JSONObject();
		try {
			PageInfo<Channel> pageInfo = channelService.pageFind(pageNum, pageSize);
			obj.put("pageInfo", pageInfo);
			obj.put("success", true);
		}catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		return obj.toString();
	}
	
	
	@RequestMapping("/getCatalogs")
	@ResponseBody
	public String getCatalogs(@RequestParam("channelId")int channelId, @RequestParam(value="pageNum", defaultValue="1") int pageNum,@RequestParam(value="sortType", defaultValue="1" ) int sortType){
		JSONObject obj = new JSONObject();
		try {
			PageInfo<Catalog> pages = catalogService.pageFindCatalogsByChannelId(channelId,pageNum, 20, sortType);
			obj.put("pages", pages);
			obj.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		return obj.toString();
	}
}
